package com.dingreading.cloud.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.dingreading.cloud.admin.entity.MemberCash;
import com.dingreading.cloud.admin.service.MemberCashService;
import com.dingreading.cloud.admin.service.StoresService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.excel.ExcelHandler;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/memberCash")
@Api(tags = "总部-会员押金管理")
public class MemberCashController {

    @Resource
    private MemberCashService memberCashService;
    @Resource
    private StoresService storesService;

    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "会员姓名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "类型 1.收款 2.退款") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(value = "活动开始日期") @RequestParam(value = "beginDate", required = false) String beginDate,
            @ApiParam(value = "活动结束日期") @RequestParam(value = "endDate", required = false) String endDate
    ) throws Exception {
        Page<MemberCash> page = memberCashService.pageList(pageUtil, null, null, keyword, null, type, beginDate, endDate);

        PageDto<MemberCash> dto = new PageDto<>();
        dto.setList(page.getRecords());
        dto.setTotal(page.getTotalRow());
        return R.ok(dto);
    }

    @ApiOperation(value = "导出自营门店数据")
    @RequestMapping(value = "/export", produces = "application/json; charset=UTF-8")
    public void export(
            HttpServletResponse response,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "活动开始日期") @RequestParam(value = "beginDate", required = false) String beginDate,
            @ApiParam(value = "活动结束日期") @RequestParam(value = "endDate", required = false) String endDate
    ) throws Exception {
        //  获取本月的第一天和最后一天
        if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate)) {
            Date monthMinDate = DateUtil.getMonthMinDate();
            Date monthMaxDate = DateUtil.getMonthMaxDate();
            beginDate = DateUtil.getDefaultTime2DateStr(monthMinDate);
            endDate = DateUtil.getDefaultTime2DateStr(monthMaxDate);
        }

        List<String> selfAndJoinCorpUidList = storesService.corpUidBySelfAndConsortium();
        List<MemberCash> list = memberCashService.listByCorpUid(selfAndJoinCorpUidList, beginDate, endDate);
        if (CollectionUtils.isEmpty(list))
            throw new DescribeException(beginDate + "至" + endDate + "，未查询到信息");

        // 表格头部列的数据
        List<List<String>> headList = new ArrayList<>();
        headList.add(Collections.singletonList("门店名称"));
        headList.add(Collections.singletonList("会员名称"));
        headList.add(Collections.singletonList("押金类型"));
        headList.add(Collections.singletonList("押金金额"));
        headList.add(Collections.singletonList("图片"));
        headList.add(Collections.singletonList("付款日期"));
        headList.add(Collections.singletonList("添加人"));
        headList.add(Collections.singletonList("添加时间"));
        // 表格内的数据
        List<List<Object>> dataList = new ArrayList<>();
        for (MemberCash cash : list) {
            List<Object> data = new ArrayList<>();
            data.add(cash.getStoreName());
            data.add(cash.getMemberName());
            data.add(cash.getTypeName());
            data.add(cash.getMoney());
            data.add(cash.getUrl());
            if (cash.getPayDate() != null) {
                String date = DateUtil.getDefaultTime2DateStr(cash.getPayDate());
                data.add(date);
            } else {
                data.add("");
            }
            data.add(cash.getAddStaffName());
            data.add(DateUtil.getDefaultTimeStr(cash.getAddTime()));

            dataList.add(data);
        }

        String fileName = "借阅押金" + beginDate + "至" + endDate;
        EasyExcel.write(ExcelHandler.getOutputStream(fileName, response))
                // 设置单元格样式
                .registerWriteHandler(ExcelHandler.getCellStyleStrategy())
                .head(headList)
                .sheet("会员服务")
                .doWrite(dataList);
    }

}
